//
//  OwnerViewController.m
//  UI16.真.豆瓣
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 Amydom. All rights reserved.
//

#import "OwnerViewController.h"
#import "OwnerViewCell.h"
#import "LoginViewController.h"
#import "OwnerActController.h"
#import "SingLeton.h"
@interface OwnerViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,retain)UITableView *myTable;

@end

@implementation OwnerViewController
-(void)dealloc{
    [_myTable release];
    [super dealloc];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"];
    if ([@"0" isEqualToString:isLogin]) {
        self.navigationItem.rightBarButtonItem.title = @"登陆";
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    } else {
        self.navigationItem.rightBarButtonItem.title = @"注销";
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    
    self.myTable = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.myTable];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    [self.myTable registerClass:[OwnerViewCell class] forCellReuseIdentifier:@"cell"];
    [_myTable release];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(RightAction:)] autorelease];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    
//去掉多余的cell
    self.myTable.tableFooterView = [UIView new];
    
}

-(void)RightAction:(UIBarButtonItem *)btn{
      NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"];
    if (![isLogin isEqualToString:@"1"]) {
        
        LoginViewController *loginC = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:loginC animated:YES];
        [loginC release];
        
    }else{ // 已登陆
        UIAlertController *alertV = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否注销" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
//            [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"name"];
            
            [[NSUserDefaults standardUserDefaults] setValue:@"0" forKey:@"isLogin"];
            
            self.navigationItem.rightBarButtonItem.title = @"登陆";
            
        }];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            return;
            
        }];
        
        [alertV addAction:action1];
        [alertV addAction:action2];
        
        [self presentViewController:alertV animated:YES completion:^{
            
        }];
        
    }

        
        
    }
    
    

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"cell";
    
    OwnerViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (indexPath.row == 0) {
    cell.textLabel.text = @"我的活动";
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"我的电影";
    }
    if (indexPath.row == 2) {
        cell.textLabel.text = @"清除缓存";
    }
    return cell;
    
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    // 取消置灰
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 点击活动
    OwnerActController *ownerAct = [[OwnerActController alloc] init];
    if (indexPath.row == 0) {
        if ([[userDefault objectForKey:@"isLogin"] isEqualToString:@"0"]) {
            LoginViewController *logVC = [[LoginViewController alloc]init];
            [self.navigationController pushViewController:logVC animated:YES];
        }else{
            [self.navigationController pushViewController:ownerAct animated:YES];
        }
        
        
    }
    [ownerAct release];

    
    
    
    
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
